/*
 * 文 件 名:  HelloController.java
 * 版    权:  kuanrf   Copyright YYYY-YYYY,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  sheng.li
 * 修改时间:  2015年8月19日
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.ls.fw.token.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.ls.fw.commons.core.exception.FwException;
import com.ls.fw.mvc.springmvc.base.BaseCRUDController;
import com.ls.fw.mvc.springmvc.test.UserModel;
import com.ls.fw.mvc.springmvc.util.ViewUtil;
import com.ls.fw.token.annotation.RequireToken;
import com.ls.fw.token.handler.HttpTokenHandler;

/**
 * <一句话功能简述>
 * <功能详细描述>
 * 
 * @author  sheng.li
 * @version  [版本号, 2015年8月19日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@Controller
@RequestMapping(value = "/hello1")
public class Hello1Controller extends BaseCRUDController
{
    
    private static final int CODE_FAIL = 400;
	private static final String CODE = "code";
	private static final int CODE_SUCCESS = 200;
	private static final String MSG = "msg";

	@Autowired
	private HttpTokenHandler httpTokenHandler;
	 
	//http://127.0.0.1:8080/backend-token-test/hello1/save1?_json=1
    @RequestMapping(value = "/save1")
    @RequireToken
    public ModelAndView save(WebRequest webRequest,Integer i) throws IOException{
        ModelAndView mav = new ModelAndView("/hello/test");
        int code = CODE_FAIL; 
        Object msg = "";
        if (i!=null) {
			throw new FwException("出错啦！");
		}
        mav.addObject(CODE, code);
        mav.addObject(MSG, msg);
        return ViewUtil.convert(mav, webRequest);
    }
    
    @RequestMapping(value = "/save2")
    public ModelAndView save2(WebRequest webRequest,Integer i) throws IOException{
        ModelAndView mav = new ModelAndView("/hello/test");
        int code = CODE_FAIL; 
        Object msg = "";
        if (i!=null) {
			throw new FwException("出错啦！");
		}
        mav.addObject(CODE, code);
        mav.addObject(MSG, msg);
        return ViewUtil.convert(mav, webRequest);
    }

    @RequestMapping(value = "/gen")
    public ModelAndView gen(WebRequest webRequest,Integer i, HttpServletRequest request, HttpServletResponse response) throws IOException{
        ModelAndView mav = new ModelAndView();
        int code = CODE_FAIL; 
        Object msg = "";
        if (i!=null) {
			throw new FwException("出错啦！");
		}
        mav.addObject(CODE, code);
        mav.addObject(MSG, msg);
        mav.addObject("data", httpTokenHandler.createToken(request, response, null));
        return ViewUtil.convert(mav, webRequest,true);
    }
   
}
